import { createLoadingComponent } from 'element-plus/es/components/loading/src/loading'
import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
    {
        path:"/login",
        name:"login",
        component:()=>import('../views/login.vue'),
        meta:{title:'登录',requireAuth:false},
    },
    {
        path:"/403",
        name:"403",
        component:()=>import('../views/403.vue'),
        meta:{title:'403 页面不存在！',requireAuth:false},
    },
    {
        path:"/Absurd",
        component:()=>import('../views/home.vue'),
        meta:{requireAuth:true},
        children: [
            {
                path:"",
                name:"home",
                component:()=>import('../views/pages/home.vue'),
                meta:{title:'首页'}
            },
            {
                path: 'about',
                component: ()=>import('../views/pages/about.vue'),
                meta:{title:'关于'}
            },
            {
                path: 'app',
                component: ()=>import('../views/pages/app.vue'),
                meta:{title:'客户端'}
            },
        ],
    },
]
//创建路由函数
const router = createRouter({ history:createWebHashHistory(),routes })
/* 路由前置守卫 */
import { useAuthStore } from '@/stores'
router.beforeEach((to,from,next)=>{
    const existrouter = router.getRoutes().find(route => route.path === to.path);//路由判定值
    if(!existrouter){
        next('/403')
    }else{
        if(to.meta.requireAuth){
            if(useAuthStore().isLoggedIn && to.path == '/login'){
                next('/Absurd')
            }else if(useAuthStore().isLoggedIn && to.path !== '/login'){
                next()
            }else{
                next('/login')
            }
        }else{
            if(useAuthStore().isLoggedIn && to.path == '/login'){
                next('/Absurd')
            }else{
                next()
            }
        }
    }
})
export default router;